//
//  SISSMorePopupView.m
//  HomeTest
//
//  Created by 优房网 on 2017/4/13.
//  Copyright © 2017年 优房网. All rights reserved.
//

#import "SISSMorePopupView.h"

#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height
@interface SISSMorePopupView ()<UITableViewDelegate,UITableViewDataSource,UIGestureRecognizerDelegate>
/** 标题数组 */
@property (copy, nonatomic) NSArray *titles;
@end
@implementation SISSMorePopupView

- (instancetype)initWithTitles:(NSArray *)titles {
    
    if (self = [super init]) {
        self.titles = titles;
        
        // UI搭建
        [self setupUI];
    }
    return self;
}

#pragma mark - UI搭建
- (void)setupUI {

    self.frame = [UIScreen mainScreen].bounds;
    self.backgroundColor = [UIColor colorWithWhite:0.5 alpha:0.5];
    [UIView animateWithDuration:0.1 animations:^{
        self.alpha = 1;
    }];
    
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureHiddenView)];
    tapGesture.delegate = self;
    [self addGestureRecognizer:tapGesture];
    
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    tableView.bounces = NO;
    tableView.dataSource = self;
    tableView.delegate = self;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self addSubview:tableView];
    
    CGSize size = CGSizeMake(94, 44 * self.titles.count);
    [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).offset(kTopHeight);
        make.right.mas_equalTo(self).offset(-8);
        make.size.mas_equalTo(size);
    }];
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
    return YES;
}


#pragma mark - 弹出此弹窗
/** 弹出此弹窗 */
- (void)show {
    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
    [keyWindow addSubview:self];
}

#pragma mark - 移除此弹窗
/** 移除此弹窗 */
- (void)dismiss{
    
    [self removeFromSuperview];
}

- (void)tapGestureHiddenView {
    
    __weak typeof(self) weakSelf = self;
    
    [UIView animateWithDuration:0.5 animations:^{
        weakSelf.alpha = 0;
    } completion:^(BOOL finished) {
        [weakSelf dismiss];
    }];
}

#pragma mark - UITableViewDelegate & UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.titles.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *reuseID = @"reuseCellID";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseID];
    UIButton *button;
    if ( !cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseID];
        button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setTitleColor:COLOR(0x334A60) forState:UIControlStateNormal];
        [button addTarget:self action:@selector(buttonClickAction:) forControlEvents:UIControlEventTouchUpInside];
        [cell.contentView addSubview:button];
        [button mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.top.equalTo(cell.contentView);
            make.bottom.equalTo(cell.contentView).offset(1);
        }];
        button.tag = indexPath.row;
        UIView *line = [[UIView alloc] init];
        line.backgroundColor = [UIColor groupTableViewBackgroundColor];
        [cell.contentView addSubview:line];
        [line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.equalTo(cell.contentView);
            make.height.mas_equalTo(1);
        }];
    }
    
    [button setTitle:self.titles[indexPath.row] forState:UIControlStateNormal];
    
    return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 44;
}

/**
 按钮的点击事件

 @param button button
 */
- (void)buttonClickAction:(UIButton *)button {
    
    
    if ([self.delegate respondsToSelector:@selector(morePopupViewSelectedWithIndex:)]) {
        
        [self.delegate morePopupViewSelectedWithIndex:button.tag];
        
        [self dismiss];
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if ([self.delegate respondsToSelector:@selector(morePopupViewSelectedWithIndex:)]) {
        
        [self.delegate morePopupViewSelectedWithIndex:indexPath.row];
        
        [self dismiss];
    }
}





@end
